<?php

	//引入配置文件
	require '../public/config.php';
	require '../public/mysql.php';
	//开启session
	session_start();

	//获取用户的操作
	$action = $_GET['a'];
	
	//判断操作
	switch ($action) {
		case 'addCart':
		//获取商品的信息
		//商品id
		$id = $_POST['gid'];
		//商品的数量
		$num = $_POST['amount'];
		//连接数据库
		$link = connect();
		if ($link['error']) {
			$sql = "select * from goods where id = '{$id}'";
			$result = query($link['mess'], $sql);
		}
		$goods = $result[0];
		if (empty($_SESSION['cart'][$id])) {
			//计算出商品的小计
			$count = $num * $goods['price'];
			//将数量和小计存到数组中
			$goods['num'] = $num;
			$goods['count'] = $count;
			$_SESSION['cart'][$id] = $goods;
		} else {
			//重新计算商品的数量
			$_SESSION['cart'][$id]['num'] += $num;
			//计算小计
			$_SESSION['cart'][$id]['count'] = $_SESSION['cart'][$id]['num'] * $goods['price'];
		}
		
		//var_dump($goods);
		header('location:./flow1.php');
		break;

		//删除购物车信息
		case 'delCart':
			//接受商品的id
			$id = $_GET['id'];
			//删除session中对应的商品
			unset($_SESSION['cart'][$id]);
			header('location:./flow1.php');
		break;
		case 'add':
    
            //接受来自表单的数据
			// var_dump($_POST);
		    $username=$_POST['username'];
			$pass=$_POST['pass'];
			$phone=$_POST['phone'];
			$email=$_POST['email'];
		
			$addtime=time();
			 $link=connect();
			  //对表单的数据进行正则验证
                 $preg='/\S+/';
				 if(@!preg_match($preg,$username)){
				         echo "<script>alert('没写用户名！');location.href='./adduser.php'</script>";
                       die();
				 }
               //验证邮箱
			   $pregEmail = '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/';
               if(@!preg_match($pregEmail,$email)){
			   echo "<script>alert('邮箱不对！');location.href='./adduser.php'</script>";
			         die();
			   }
               //验证手机号
			   $pregPhone = '/^1(3|4|5|7|8)\d{9}$/';
               if(@!preg_match($pregPhone,$phone)){
			   echo "<script>alert('手机号不对！');location.href='./adduser.php'</script>";
			         die();
			   }
			$sql="insert into vip(username,pass,phone,email,role,rules,addtime) value('{$username}','{$pass}','{$phone}','{$email}',0, 0, '{$addtime}')";
			// echo $sql;die;
			//发送 $sql语句并执行
			$result=excute($link['mess'], $sql);

			//根据执行的结果，返回不同的页面
			if ($result['error']) {
			echo "<script>alert('注册成功');location.href='./login.php'</script>";
					die();
			} else {
				echo "<script>alert('注册失败');location.href='./adduser.php'</script>";
					die();;
			}
		break;
		//退出操作
		case 'logout':
			session_start();
			//销毁session
			unset($_SESSION['admin']['user']);
			header('location:../index.php');
		break;
		case 'delCart1':
			//接受商品的id
			$id = $_GET['id'];
			//删除session中对应的商品
			unset($_SESSION['cart'][$id]);
			header('location:./flow2.php');
		break;
		case 'address_edit':
			$id=$_POST['id'];
			$cons=$_POST['cons'];
			$phone=$_POST['phone'];
			$addr=$_POST['addr'];
		
			//连接数据库
			$link = connect();
			if ($link['error']) {
				$sql = "update address set consignee='{$cons}',phone ='{$phone}',addr='{$addr}' where id = {$id}";
				$result = excute($link['mess'], $sql);
			}
			if($result['error']){
				echo "<script>alert('修改成功');location.href='./flow2.php'</script>";
			}
		break;
		case 'submit':
			$webUsername = $_SESSION['admin']['user'];
			//$addid为flow2表单提交过来的地址ID
			$addid = $_POST['adds'];
//			var_dump($addid);die;
			$cart = ($_SESSION['cart']);
//		var_dump($addid);die;
//	var_dump($cart);die;
			foreach($cart as $val){
				$price=$val['price'];
				$name=$val['name'];
			}
//			echo $price;
		
			$order_number = mt_rand(1000000,9999999);
			$ctime = date('Y-m-d H:i:s',time());
			//将商品信息ID、商品的数量存入订单详情表中
			$link = connect();
			foreach($cart as $val){
				if ($link['error']){
					$sql = "insert into detail() values(NULL,'{$order_number}','{$webUsername}','{$price}','{$val['num']}','{$name}','{$addid}')";
//					echo $sql;die;
					excute($link['mess'], $sql);
				}
				$Allcount+=$val['count'];
			}
			//将订单号、用户名、地址ID、创建时间存入表中
			$link = connect();
			if ($link['error']){
				$sql = "insert into submit() values(NULL,'{$webUsername}','{$addid}','{$ctime}')";
				excute($link['mess'], $sql);
			}
			if($link['error']){
				//删除$_SESSION['cart']购物车信息
				unset($_SESSION['cart']);
				header('location:./flow3.php');
			}
		break;
		case 'pass':
			$id=$_POST['id'];
			$username=$_POST['username'];
			$pass=$_POST['pass'];



              $link=connect();
             
              $sql="update user set username='{$username}',pass='{$pass}' where id = $id";
//              echo $sql;
//              die();
   			if ($link['error']) {
				$result = excute($link['mess'], $sql);
				if ($result['error']) {
					echo "<script>alert('修改成功');location.href='./userList.php'</script>";
					die();
				}
			}
		      

			//根据执行的结果，返回不同的页面
			if ($result['error']) {
				header('location:./userList.php');
				
			} else {
				header('location:./userEdit.php');
			}
           break;

		   case 'addvip':
    
            //接受来自表单的数据
			// var_dump($_POST);
		    @$cons=$_POST['consignee'];
			var_dump($cons);
			$phone=$_POST['phone'];
			//var_dump($phone);
			$addr=$_POST['addr'];
			//var_dump($addr);
			$user= $_SESSION['admin']['user'] ;
		
//			var_dump($user);
			 $link=connect();

			$sql="insert into address(consignee,phone,addr,pid) values('{$cons}','{$phone}','{$addr}','{$user}')";
			
			//发送 $sql语句并执行
			$result=excute($link['mess'], $sql);
//			var_dump($result);
			//根据执行的结果，返回不同的页面
			if ($result['error']) {
			echo "<script>alert('添加成功');location.href='./flow2.php'</script>";
					die();
			} else {
				echo "<script>alert('添加失败');location.href='./ad.php'</script>";
					die();;
			}
		break;
	}

?>